//
//  EJSInviteFriendViewController.m
//  WestEurope
//
//  Created by lifayu on 2018/6/29.
//  Copyright © 2018年 lifayu. All rights reserved.
//

#import "EJSInviteFriendViewController.h"
#import "EJShareContentRequest.h"
#import "UIView+Ext.h"

@interface EJSInviteFriendViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *userIconImageView;
@property (weak, nonatomic) IBOutlet UILabel *userNameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *shareCodeImageView;
@property (weak, nonatomic) IBOutlet UIView *contentView;

@property (strong, nonatomic) EJSShareContentModel *shareContentModel;

@end

@implementation EJSInviteFriendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"邀请好友";
    [self loadShareContent];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)configOwnViews{
    EJSUserModel *userInfo = [EJSUserModel userModel];
    
    [self.userIconImageView setImageWithUrl:userInfo.avatar PlaceHolder:[UIImage imageNamed:@"user_btn"]];
    self.userNameLabel.text = userInfo.name?userInfo.name:userInfo.phone;
}

- (void)loadShareContent{
    FlyWeakSelf
    [[EJShareContentRequest new] asyncRequestWithsuccessHandler:^(BaseResponse *response) {
        weakSelf.shareContentModel = response.data;
    } failHandler:^(BaseResponse *response) {
        [[HUDHelper sharedInstance] tipMessage:response.message];
    }];
}

- (void)setShareContentModel:(EJSShareContentModel *)shareContentModel{
    _shareContentModel = shareContentModel;
    [self.shareCodeImageView setImageWithUrl:shareContentModel.qrcode PlaceHolder:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)shareAction:(UIButton *)sender {
    //分享的标题
    NSString *textToShare = @"漫时光-商城";
    //分享的图片
    UIImage *imageToShare = [self.contentView snapshotImage];
    //分享的url
    NSURL *urlToShare = [NSURL URLWithString:self.shareContentModel.url];
    //在这里呢 如果想分享图片 就把图片添加进去  文字什么的通上
    NSArray *activityItems = @[textToShare,imageToShare, urlToShare];
    UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];
    //不出现在活动项目
    activityVC.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeCopyToPasteboard,UIActivityTypeAssignToContact,UIActivityTypeSaveToCameraRoll];
    [self presentViewController:activityVC animated:YES completion:nil];
    // 分享之后的回调
    activityVC.completionWithItemsHandler = ^(UIActivityType  _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
        if (completed) {
            [[HUDHelper sharedInstance] tipMessage:@"分享成功"];
        } else  {
            [[HUDHelper sharedInstance] tipMessage:@"分享失败"];
        }
    };
}

- (IBAction)saveAction:(UIButton *)sender {
    UIImage *image = [self.contentView snapshotImage];
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    if(!error){
        [[HUDHelper sharedInstance] tipMessage:@"保存图片到相册"];
    }
}

@end
